<?php

namespace App\Jobs;

use App\Jobs\Job;
use App\Facades\DeviceService;
use App\Facades\UserManager;
use Mail;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendTemplateMessage extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    private $_data;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->_data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $wxUsers = UserManager::getWxUserByDeviceCode($this->_data['deviceCode']);
        if(!is_null($wxUsers))
        {
            foreach($wxUsers as $user)
            {
                DeviceService::sendTemplateMessage($user, $this->_data);
            }
        }
    }

}
